var menuTree = Vue.extend({
  name: 'menu-tree',
  template:[
    '<div style="border: 1px solid #e6e6e6"><div id="menu_tree" class="xtree_contianer"></div></div>'
  ].join('')
});

Vue.component('menuTree', menuTree);

var menuTreeList = []
var menuTreeModel = '';

function getMenuList () {
    let list = [];
    var oCks = menuTreeModel.GetChecked();
    for (var i = 0; i < oCks.length; i++) {
        list.push(oCks[i].value)
    }
    return list
}

$(function () {
  layui.use(['form'], function () {
      var form = layui.form;
      $.ajax({
          url: '/menu/list',
          success: function (res) {
              menuTreeList = res.data;
              menuTreeModel = new layuiXtree({
                  elem: 'menu_tree'
                  , form: form
                  , data: eachList(menuTreeList)
              });
          }
      })
  });
})

function eachList (data) {
    var list = []
    $.each(data, function (i, t) {
        let obj = {}
        obj.title = t.name
        obj.value = t.id
        if (t.children.length > 0) {
            obj.data = eachList(t.children)
        } else {
            obj.data = []
        }
        list.push(obj)
    })
    return list
}

// 数据回显
function menuTreeLoad (data) {
    for (let i in data) {
        $('.layui-form input').each(function (m, item) {
            if ($(this).val() == data[i].id && $(this).attr('data-xend') == 1) {
              console.log("$(this).val(): ", $(this).val())
                $(this).siblings('div').click()
            }
        })
    }
}
